<?php
/*
 * @ https://EasyToYou.eu - IonCube v11 Decoder Online
 * @ PHP 7.2 & 7.3
 * @ Decoder version: 1.1.6
 * @ Release: 10/08/2022
 */

// Decoded file for php version 71.
namespace app\adapay\controller;

class CallBack extends \app\ApiRest
{
    protected $app;
    public function __construct(\think\App $app)
    {
        $this->app = $app;
    }
    public function companyCallback()
    {
        $this->request = $this->app->request;
        $inputs = json_decode($this->request->getInput(), true);
        if(empty($inputs)) {
            $inputs = $_POST;
        }
        $callbak_model = new \app\adapay\model\Callback();
        $data = json_decode($inputs["data"], true);
        $insert = ["uniacid" => 666, "adapay_id" => $data["member_id"], "status" => $inputs["type"], "type" => "company", "text" => serialize($inputs)];
        $callbak_model->dataAdd($insert);
        $member_model = new \app\adapay\model\Member();
        $find = $member_model->dataInfo(["status" => 0, "member_id" => $data["member_id"]]);
        if(!empty($find)) {
            if(in_array($data["audit_state"], ["D", "E"])) {
                $member_model->dataUpdate(["id" => $find["id"]], ["status" => 1]);
            } else {
                $member_model->dataUpdate(["id" => $find["id"]], ["status" => 2, "failure_reason" => $data["audit_desc"]]);
            }
        }
        $res = ["code" => 0, "msg" => "成功"];
        echo json_encode($res);
        exit;
    }
    public function refundCallback()
    {
        $this->request = $this->app->request;
        $inputs = json_decode($this->request->getInput(), true);
        if(empty($inputs)) {
            $inputs = $_POST;
        }
        $callbak_model = new \app\adapay\model\Callback();
        $data = json_decode($inputs["data"], true);
        $dis = ["adapay_id" => $data["id"], "type" => "refund"];
        $find = $callbak_model->dataInfo($dis);
        if(empty($find)) {
            $insert = ["uniacid" => 666, "adapay_id" => $data["id"], "status" => $inputs["type"], "type" => "refund", "text" => serialize($inputs)];
            $callbak_model->dataAdd($insert);
        }
        $pay_model = new \app\adapay\model\PayRecord();
        $refund_model = new \app\massage\model\RefundOrder();
        $log_model = new \app\massage\model\OrderPrice();
        $record = $pay_model->dataInfo(["adapay_id" => $data["id"], "type" => "refund"]);
        $cash = $record["pay_price"];
        if(!empty($record) && $record["status"] != 2) {
            if($data["status"] == "succeeded") {
                $pay_model->dataUpdate(["adapay_id" => $data["id"]], ["status" => 2]);
                if(!empty($record["order_id"])) {
                    $refund_model->where(["id" => $record["order_id"]])->update(["have_price" => \think\facade\Db::Raw("have_price+" . $cash), "out_refund_no" => $data["id"]]);
                    $refund_order = $refund_model->dataInfo(["id" => $record["order_id"]]);
                    if(round($refund_order["refund_price"], 2) <= round($refund_order["have_price"], 2) && $refund_order["status"] == 4) {
                        $refund_model->passOrderData($record["order_id"], $refund_order["have_price"], 2, $refund_order["check_user"], 0, 2, 0, $refund_order["is_admin"]);
                    }
                }
            }
            if($data["status"] == "failed") {
                $refund_model->dataUpdate(["id" => $record["order_id"]], ["status" => 5, "failure_reason" => $data["error_msg"]]);
                $pay_model->where(["id" => $record["pay_record_id"]])->update(["true_price" => \think\facade\Db::raw("true_price+" . $cash)]);
                $log_model->where(["id" => $record["log_id"]])->update(["can_refund_price" => \think\facade\Db::raw("can_refund_price+" . $cash)]);
            }
        }
        $res = ["code" => 0, "msg" => "成功"];
        echo json_encode($res);
        exit;
    }
    public function upOrderRefundCallback()
    {
        $this->request = $this->app->request;
        $inputs = json_decode($this->request->getInput(), true);
        if(empty($inputs)) {
            $inputs = $_POST;
        }
        $data = json_decode($inputs["data"], true);
        $pay_model = new \app\adapay\model\PayRecord();
        $refund_model = new \app\massage\model\UpRefundOrder();
        $record = $pay_model->dataInfo(["adapay_id" => $data["id"], "type" => "up_refund"]);
        $cash = $record["pay_price"];
        if($data["status"] == "succeeded") {
            $pay_model->dataUpdate(["adapay_id" => $data["id"]], ["status" => 2]);
            $refund_model->dataUpdate(["id" => $record["order_id"]], ["status" => 2, "have_price" => $cash, "transaction_id" => $data["id"]]);
        }
        if($data["status"] == "failed") {
            $refund_model->dataUpdate(["id" => $record["order_id"]], ["status" => 5, "failure_reason" => $data["error_msg"]]);
            $pay_model->where(["id" => $record["pay_record_id"]])->update(["true_price" => \think\facade\Db::raw("true_price+" . $cash)]);
        }
        return true;
    }
    public function payCallback()
    {
        $this->request = $this->app->request;
        $inputs = json_decode($this->request->getInput(), true);
        if(empty($inputs)) {
            $inputs = $_POST;
        }
        $callbak_model = new \app\adapay\model\Callback();
        $data = json_decode($inputs["data"], true);
        if($data["status"] == "succeeded") {
            $pay_record_model = new \app\adapay\model\PayRecord();
            $dis = ["adapay_id" => $data["id"]];
            $record = $pay_record_model->dataInfo($dis);
            $pay_record_model->dataUpdate($dis, ["status" => 1]);
            if(!empty($record)) {
                if($record["type"] == "Balance") {
                    $order_model = new \app\massage\model\BalanceOrder();
                    $order_model->orderResult($record["order_code"], $data["id"]);
                } elseif($record["type"] == "Massage") {
                    $order_model = new \app\massage\model\Order();
                    $order_model->orderResult($record["order_code"], $data["id"]);
                } elseif($record["type"] == "MassageUp") {
                    $order_model = new \app\massage\model\UpOrderList();
                    $order_model->orderResult($record["order_code"], $data["id"]);
                } elseif($record["type"] == "Adapay") {
                    $order_model = new \app\adapay\model\AdminPay();
                    $order_model->orderResult($record["order_code"], $data["id"]);
                } elseif($record["type"] == "ResellerPay") {
                    $order_model = new \app\payreseller\model\Order();
                    $order_model->orderResult($record["order_code"], $data["id"]);
                } elseif($record["type"] == "AgentRecharge") {
                    $order_model = new \app\mobilenode\model\RechargeList();
                    $order_model->orderResult($record["order_code"], $data["id"]);
                } elseif($record["type"] == "Memberdiscount") {
                    $order_model = new \app\memberdiscount\model\OrderList();
                    $order_model->orderResult($record["order_code"], $data["id"]);
                } elseif($record["type"] == "Balancediscount") {
                    $order_model = new \app\balancediscount\model\OrderList();
                    $order_model->orderResult($record["order_code"], $data["id"]);
                }
            }
        }
        $dis = ["adapay_id" => $data["id"]];
        $find = $callbak_model->dataInfo($dis);
        if(empty($find)) {
            $insert = ["uniacid" => 666, "adapay_id" => $data["id"], "status" => $inputs["type"], "type" => "pay", "text" => serialize($inputs), "out_trans_id" => !empty($data["out_trans_id"]) ? $data["out_trans_id"] : "", "party_order_id" => !empty($data["party_order_id"]) ? $data["party_order_id"] : ""];
            $callbak_model->dataAdd($insert);
        }
        $res = ["code" => 0, "msg" => "成功"];
        echo json_encode($res);
        exit;
    }
    public function walletCallback()
    {
        $this->request = $this->app->request;
        $inputs = json_decode($this->request->getInput(), true);
        if(empty($inputs)) {
            $inputs = $_POST;
        }
        $callbak_model = new \app\adapay\model\Callback();
        $data = json_decode($inputs["data"], true);
        $wallet_model = new \app\massage\model\Wallet();
        $dis = ["payment_no" => $data["id"]];
        $record = $wallet_model->dataInfo($dis);
        if($data["status"] == "succeeded" && !empty($record)) {
            $wallet_model->dataUpdate(["id" => $record["id"]], ["status" => 2]);
        }
        if($data["status"] == "failed" && !empty($record)) {
            $wallet_model->dataUpdate(["id" => $record["id"]], ["status" => 5, "failure_reason" => $data["error_msg"]]);
        }
        $insert = ["uniacid" => 666, "adapay_id" => $data["id"], "status" => $inputs["type"], "type" => "wallet", "text" => serialize($inputs)];
        $callbak_model->dataAdd($insert);
        $res = ["code" => 0, "msg" => "成功"];
        echo json_encode($res);
        exit;
    }
}

?>